package public

import (
	"gitee.com/wanjimao/dongli-kit/mq/miface"
	"gitee.com/wanjimao/dongli-kit/mq/pkg/mfx"
	"gitee.com/wanjimao/dongli-kit/orm/nosql/diface"
	"gitee.com/wanjimao/dongli-kit/orm/pkg/ofx"
	"gitee.com/wanjimao/dongli-kit/pkg/mongopkg"
	"gitee.com/wanjimao/dongli-kit/pkg/mysqlpkg"
	"gitee.com/wanjimao/dongli-kit/pkg/setting"
	"gitee.com/wanjimao/dongli-kit/server/pkg/sfx"
	"gitee.com/wanjimao/dongli-kit/server/siface"
	"gitee.com/wanjimao/dongli-util/dancetiktok/model/dancetiktokmodelmysql"
	"github.com/redis/go-redis/v9"
	"go.mongodb.org/mongo-driver/mongo"
	"go.uber.org/fx"
	"go.uber.org/zap"

	pb "gitee.com/wanjimao/dongli-platform/api/gen/profile/api"
	"gitee.com/wanjimao/dongli-platform/services/profile/internal/db"
	"gitee.com/wanjimao/dongli-platform/services/profile/pkg/pfx"
)

type Service struct {
	logger         *zap.Logger
	db             *db.Database
	redisCli       *redis.Client
	mongoCli       *mongo.Client
	mq             miface.MessageQueue
	authMiddleware siface.IAuthMiddleware
	set            setting.Param
	mysqlDb        mysqlpkg.Param
	mongoDb        mongopkg.Param
}

func (s *Service) RegisterWithGrpcServer(server siface.IGrpcServer) error {
	pb.RegisterProfileServiceServer(server.GrpcServer(), s)
	return nil
}

func NewService(
	l *zap.Logger,
	coll diface.ICollection,
	client *redis.Client,
	redisCache diface.ICache,
	mongoCli *mongo.Client,
	mq miface.MessageQueue,
	authMiddleware siface.IAuthMiddleware,
	mysqlDb mysqlpkg.Param,
	set setting.Param,
	mongoDb mongopkg.Param,
) (result *Service, err error) {
	result = &Service{
		logger:         l,
		db:             db.OpenDatabase(l, coll, redisCache),
		redisCli:       client,
		mongoCli:       mongoCli,
		mq:             mq,
		authMiddleware: authMiddleware,
		mysqlDb:        mysqlDb,
		set:            set,
		mongoDb:        mongoDb,
	}
	return
}

var Module = fx.Provide(
	func(
		l *zap.Logger,
		pSetting pfx.ProfileSettingParams,
		dbProvider ofx.DocumentStoreParams,
		redisParams ofx.RedisParams,
		rcParams ofx.RedisCacheParams,
		dbParams ofx.MongoParams,
		mqParams mfx.MessageQueueParams,
		authMiddlewareParams sfx.AuthMiddlewareParams,
		mysqlDb mysqlpkg.Param,
		set setting.Param,
		mongoDb mongopkg.Param,
	) (out sfx.GrpcServiceResult, err error) {

		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceUserAccount{}); nil != err {
			panic(err)
		}
		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceUserAccountRole{}); nil != err {
			panic(err)
		}
		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceRandName{}); nil != err {
			panic(err)
		} else {
		}
		//频道表
		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceChannel{}); nil != err {
			panic(err)
		} else {
		}
		//频道表
		//if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceElement{}); nil != err {
		//	panic(err)
		//} else {
		//}
		//背包表表
		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceBag{}); nil != err {
			panic(err)
		} else {
		}
		//背包详情表
		if err := mysqlDb.MysqlDanceDb.AutoMigrate(&dancetiktokmodelmysql.DanceBagDetail{}); nil != err {
			panic(err)
		} else {
		}
		if coll, e := dbProvider.DriverProvider.OpenDbDriver(pSetting.ProfileStoreName); e != nil {
			err = e
		} else {
			if svc, e := NewService(
				l,
				coll,
				redisParams.Redis,
				rcParams.RedisCache,
				dbParams.MongoClient,
				mqParams.MessageQueue,
				authMiddlewareParams.AuthMiddleware,
				mysqlDb,
				set,
				mongoDb,
			); e != nil {
				err = e
			} else {
				out.GrpcService = svc
			}
		}
		return
	},
)
